package com.example.lianguisystem.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.example.lianguisystem.common.Result;
import com.example.lianguisystem.mapperLianGui.AppMessageMapper;
import com.example.lianguisystem.utils.OkHttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private AppMessageMapper appMessageMapper;


    // saveCid 生产一个saveCid接收请求方法，是get请求，参数有username和cid
    @RequestMapping("/saveCid")
    public Result saveCid(@RequestParam("username") String username,@RequestParam("cid")  String cid) {
        Integer count = appMessageMapper.selectCount(username, cid);
        if (count != null && count > 0) {
            appMessageMapper.updateByUsername(username, cid);
            return Result.success();
        }
        appMessageMapper.insertInto(username, cid);
        return Result.success();
    }

    
    
    // 向指定cid发送消息
//    @RequestMapping("/sendMessage")
//    public Result sendMessage(@RequestParam("cid") String cid,
//                              @RequestParam("title") String title,
//                              @RequestParam("content") String content,
//                              @RequestParam(value = "extras", required = false) String extras) {
//        return uniPushService.sendMessageByCid(cid, title, content, extras);
//    }

    // 根据username获取cid并发送消息
    @PostMapping(value = "/send")
    public Result sendMessageByUsername(@RequestBody Map<String, Object> paramsMap) {
        List<String> usernameList =(List<String>) paramsMap.get("username");
        String title =(String) paramsMap.get("title");
        String content =(String) paramsMap.get("content");
        try {
            List<String> cidList = new ArrayList<>();
            // 根据username查询cid
            for (String username : usernameList) {
                String cid = appMessageMapper.selectCidByUsername(username);
                if (StrUtil.isNotBlank(cid)) {
                    cidList.add(cid);
                }
            }
            if (cidList.size() == 0) {
                return new Result(false, null, "发送失败: 该用户尚未绑定设备cid", null, null);
            }

            // 构建请求参数
            JSONObject requestParam = new JSONObject();
            requestParam.put("cid", cidList);
            requestParam.put("title", title);
            requestParam.put("content", content);

            // 发起POST请求到指定URL
            String result = OkHttpUtil.postJson(
                    "https://fc-mp-b8c9287a-d348-4f3c-849a-af23f1851266.next.bspapp.com/send",
                    requestParam.toJSONString());

            // 解析返回结果
            JSONObject resultJson = JSONObject.parseObject(result);
            String errMsg = resultJson.getString("errMsg");

            if ("success".equals(errMsg)) {
                return new Result(true, null, "发送成功", null, null);
            } else {
                return new Result(false, null, "发送失败: "+ resultJson, null, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, null, "发送失败: "+  e.getMessage(), null, null);
        }
    }

}
